package com.example.shop_2305.fg

import androidx.lifecycle.viewModelScope
import com.example.lib_base.entity.CartEntity
import com.example.lib_base.entity.PreOrderEntity
import com.example.lib_base.entity.Res
import com.example.lib_base.mvvm.BaseViewModel
import com.example.lib_base.net.SingleLiveData
import com.example.lib_base.param.PPreOrder
import kotlinx.coroutines.launch

class CartViewModel:BaseViewModel() {
    //请求购物车列表
    val cartSuccess = SingleLiveData<Res<CartEntity>>()
    val cartFail = SingleLiveData<String>()
    fun cart(isValid:String,limit:String,page:String){
        viewModelScope.launch {
            try {
                cartSuccess.postValue(apiService.cart(isValid, limit, page))
            } catch (e: Exception) {
                cartFail.postValue(e.message.toString())
            }
        }
    }

    //修改购物车数量
    val updateSuccess = SingleLiveData<Res<String?>>()
    val updateFail = SingleLiveData<String>()
    fun cartUpdate(id:Int,number:Int){
        viewModelScope.launch {
            try {
                updateSuccess.postValue(apiService.cartUpdate(id, number))
            } catch (e: Exception) {
                updateFail.postValue(e.message.toString())
            }
        }
    }

    //删除购物车
    val deleteSuccess = SingleLiveData<Res<String?>>()
    val deleteFail = SingleLiveData<String>()
    fun cartDelete(ids:Array<Int>){
        viewModelScope.launch {
            try {
                deleteSuccess.postValue(apiService.cartDelete(ids))
            } catch (e: Exception) {
                deleteFail.postValue(e.message.toString())
            }
        }
    }
    //提交订单---预下单
    val preOrderSuccess = SingleLiveData<Res<PreOrderEntity>>()
    val preOrderFail = SingleLiveData<String>()
    fun preOrder(pPreOrder: PPreOrder){
        viewModelScope.launch {
            try {
                preOrderSuccess.postValue(apiService.preOrder(pPreOrder))
            } catch (e: Exception) {
               preOrderFail.postValue(e.message.toString())
            }
        }
    }
}